import axios from "axios";
import vue from "vue";
import {
  setSession,
  getSession,
  getToken,
  setToken,
} from './auth'


const service = axios.create({
  timeout: 5000000 // request timeout
})
// 添加请求拦截器
service.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  // Do something before request is sent
  if (getSession()) {
    // 让每个请求携带token-- ['X-Litemall-Admin-Token']为自定义key 请根据实际情况自行修改
    config.headers['Authorization'] = getSession() || ''
  }
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

const request = function (loadtip, query) {
  let loading;
  if (loadtip) {
    loading = vue.prototype.$loading({
      lock: false,
      text: "正在加载中…",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.5)"
    });
  }
  return service
    .request(query)
    .then(res => {
      if (loadtip) {
        loading.close();
      }
      if (res.data.code === 401) {
        setSession('')
        location.reload()
        // vue.prototype.$$router.push({
        //   path: "/login"
        // });
        return Promise.reject(res.data);
      } else if (res.data.code === 500) {
        return Promise.reject(res);
      } else if (res.data.code === 501) {
        return Promise.reject(res.data);
      } else if (res.data.code === 502) {
        vue.prototype.$$router.push({
          path: "/login"
        });
        return Promise.reject(res.data);
      } else {
        return Promise.resolve(res.data);
      }
    })
    .catch(e => {
      if (loadtip) {
        loading.close();
      }
      if (e.data.code != 500) {
        vue.prototype.$message.error(e.data.message);
      }
      console.log(e.config.url)
      return Promise.reject(e.data.message);
    });
};

const post = function (url, params) {
  const query = {
    url: url,
    method: "post",
    withCredentials: true,
    timeout: 30000,
    data: params,
    headers: {
      "Content-Type": "application/json",
      "request-ajax": true
    }
  };
  return request(false, query);
};

const postWithLoadTip = function (url, params) {
  const query = {
    url: url,
    method: "post",
    withCredentials: true,
    timeout: 30000,
    data: params,
    headers: {
      "Content-Type": "application/json",
      "request-ajax": true
    }
  };
  return request(true, query);
};

const postWithOutLoadTip = function (url, params) {
  const query = {
    url: url,
    method: "post",
    withCredentials: true,
    timeout: 30000,
    data: params,
    headers: {
      "Content-Type": "application/json",
      "request-ajax": true
    }
  };
  return request(false, query);
};

const get = function (url, params) {
  const query = {
    url: url,
    method: "get",
    withCredentials: true,
    timeout: 30000,
    params: params,
    headers: {
      "request-ajax": true
    }
  };
  return request(false, query);
};

const form = function (url, params) {
  const query = {
    url: url,
    method: "post",
    withCredentials: true,
    timeout: 30000,
    data: params,
    headers: {
      "Content-Type": "multipart/form-data",
      "request-ajax": true
    }
  };
  return request(false, query);
};

export {
  post,
  postWithLoadTip,
  postWithOutLoadTip,
  get,
  form
};
